#include "nvs.h"

Preferences prefs; // 声明Preferences对象

Nvs::Nvs(/* args */)
{
}

Nvs::~Nvs()
{
}

void Nvs::initconfig()
{
    if (!prefs.begin("Keyboard"))
    {
       Serial.println("Failed to initialise NAMES");
       Serial.println("Restarting...");
       delay(10000);
       ESP.restart();
    }
    if (!prefs.isKey("keyboardtype"))
    {
       Serial.println("write");
       prefs.putUInt("keyboardtype",keyboardtype);
       prefs.putUInt("layerSN",layerSN);
       prefs.putUInt("RGBstyle",RGBstyle);
       
    }
    else
    {
       Serial.println("read");
       keyboardtype = prefs.getUInt("keyboardtype");
       layerSN = prefs.getUInt("layerSN");
       RGBstyle = prefs.getUInt("RGBstyle");
       Serial.print("keyboardtype:");
       Serial.println(keyboardtype);
       Serial.print("layerSN:");
       Serial.println(layerSN);
       Serial.print("RGBstyle:");
       Serial.println(RGBstyle);
    }
    prefs.end();
}
bool Nvs::setkeyboardtype(uint8_t utype)
{
    Serial.println("修改keyboardtype");
    Serial.print("utype:");
    Serial.println(utype);
    if (!prefs.begin("Keyboard"))
    {
       Serial.println("Failed to initialise NAMES");
       Serial.println("Restarting...");
       delay(10000);
       ESP.restart();
    }
    prefs.putUInt("keyboardtype",utype);
    prefs.end();
    Serial.println("Failed to initialise NAMES");
    Serial.println("Restarting...");
    esp_restart();
}

bool Nvs::setkeyboardlayerSN(uint8_t utype)
{
    Serial.println("layerSN");
    Serial.print("utype:");
    Serial.println(utype);
    if (!prefs.begin("Keyboard"))
    {
       Serial.println("Failed to initialise NAMES");
       Serial.println("Restarting...");
       delay(10000);
       ESP.restart();
    }
    prefs.putUInt("layerSN",utype);
    prefs.end();
    Serial.println("Failed to initialise NAMES");
    Serial.println("Restarting...");
    //Serial.println("layerSN end");
    //esp_restart();
}

bool Nvs::setkeyboardRGBstyle(uint8_t utype)
{
    Serial.println("RGBstyle");
    Serial.print("utype:");
    Serial.println(utype);
    if (!prefs.begin("Keyboard"))
    {
       Serial.println("Failed to initialise NAMES");
       Serial.println("Restarting...");
       delay(10000);
       ESP.restart();
    }
    if (!prefs.putUChar("RGBstyle", utype))
       Serial.println(" putUChar Failed ");
    prefs.end();
    
    Serial.println("Restarting...");
    //Serial.println("layerSN end");
    //esp_restart();
}